package com._13read.rss.scheduled;

import cn.hutool.core.date.SystemClock;
import com._13read.rss.entity.RssSource;
import com._13read.rss.manager.IRssManager;
import com._13read.rss.model.MyPage;
import com._13read.rss.service.IRssSourceService;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Slf4j
@Component
public class RssScheduled {

    @Autowired
    private IRssSourceService rssSourceService;
    @Autowired
    private IRssManager rssManager;

    /**
     * 定时任务 订阅rss
     */
    @Scheduled(cron = "7 7 0/1 * * ? ")
    public void subscriber(){
        long start = SystemClock.now();
        log.info("执行定时任务==>订阅rss");
        long current = 1L;
        while (true) {
            MyPage<RssSource> all = rssSourceService.all(new MyPage(current++, 20));
            List<RssSource> records = all.getRecords();
            if (CollectionUtils.isEmpty(records)) break;
            for (RssSource source : records) {
                rssManager.parse2db(source);
            }
        }
        long end = SystemClock.now();
        log.info("执行定时任务==>订阅rss==>总用时:{}",end - start);
    }
}
